# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.


rm(list = ls())

library(tidyverse)
library(xtable)
library(janitor)

study_1_mturk <- read_rds("data/clean/study_1_mturk_cleaned.rds")
study_1_elite <- read_rds("data/clean/study_1_elite_cleaned.rds")
study_1_lucid <- read_rds("data/clean/studies_1_2_replications_lucid_cleaned.rds")

study_1_mturk <- study_1_mturk %>% filter(Z != "climate")
study_1_lucid <- study_1_lucid %>% filter(Z != "climate")

study_1_stacked <-
  bind_rows(list(
    MTurk = dplyr::select(study_1_mturk, age_5, educ_5, race_4, pid_7, ideo_5, female),
    Elites = dplyr::select(study_1_elite, age_5, educ_5, race_4, pid_7, ideo_5, female),
    Lucid = dplyr::select(study_1_lucid, age_5, educ_5, race_4, pid_7, ideo_5, female)
  ), .id = "sample") %>%
  mutate(sample = factor(sample, levels = c("MTurk", "Elites", "Lucid")))


age_tab <-
study_1_stacked %>%
  tabyl(age_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

educ_tab <-
study_1_stacked %>%
  tabyl(educ_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

race_tab <-
  study_1_stacked %>%
  tabyl(race_4, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

pid_tab <-
  study_1_stacked %>%
  mutate(pid_7 = factor(pid_7, levels = 1:7,
                        labels = c("Strong Democrat",
                                   "Not very strong Democrat",
                                   "Lean Democrat",
                                   "Independent",
                                   "Lean Republican",
                                   "Not very strong Republican",
                                   "Strong Republican"))) %>%
  tabyl(pid_7, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

ideo_tab <-
  study_1_stacked %>%
  tabyl(ideo_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()


female_tab <-
  study_1_stacked %>%
  mutate(female = factor(female, levels = c(0, 1), labels = c("Male", "Female"))) %>%
  tabyl(female, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()


n_tab <-
study_1_stacked %>%
  mutate(N = "N") %>%
  tabyl(N, sample)
  

xtable(age_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_age.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(educ_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_educ.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
xtable(race_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_race.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
xtable(pid_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_pid.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
xtable(ideo_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_ideo.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
xtable(female_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_female.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
xtable(n_tab, digits = 0) %>% 
  print.xtable(
  file = "../Drafts/tables/study_1_n.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
